﻿using System.Collections.Generic;
using IDAL;
using Model;
using NHibernate;

namespace SqlDALLibrary
{
    public class CategoryDal : ICategoryDAL
    {
        private static EntityControl control;

        static CategoryDal()
        {
            control = EntityControl.CreateEntityControl("Model");
        }

        public void saveCategory(Category category)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Save(category);
                tx.Commit();
            }
            session.Close();
        }

        public void delCategory(Category category)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Delete(category);
                tx.Commit();
            }
            session.Close();
        }

        public void updateCategory(Category category)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Update(category);
                tx.Commit();
            }
            session.Close();
        }

        public Category getCategory(int id)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            Category category = session
                   .CreateQuery("from Category c where c.id=:id")
                .SetInt32("id", id)
                .UniqueResult<Category>();
            session.Close();
            return category;
        }

        public Category getCategory(string categoryName)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            Category category = session.CreateQuery("from Category c where c.categoryName=:categoryName")
                    .SetString("categoryName", categoryName)
                    .UniqueResult<Category>();
            session.Close();
            return category;
        }

        public Category getCategoryByChineseName(string chineseName)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            Category category = session.CreateQuery("from Category c where c.chineseName=:chineseName")
                    .SetString("chineseName", chineseName)
                    .UniqueResult<Category>();
            session.Close();
            return category;
        }

        public Category getCategoryByOrder(int categoryOrder)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            Category category = session
                   .CreateQuery("from Category c where c.categoryOrder=:categoryOrder")
                .SetInt32("categoryOrder", categoryOrder)
                .UniqueResult<Category>();
            session.Close();
            return category;
        }

        public IList<Category> getCategory()
        {
            return control.getSession()
                .CreateQuery("from Category order by categoryOrder")
                .List<Category>();
        }

    }
}
